<!DOCTYPE html>
<html>
<head>    
    <title>user roles</title>  
    <script type="text/javascript" src="../javascript/page/Child.js"></script>
    <style type="text/css">
        html, body {
            font-size: 12px;  background-color: #f1f1f1;
        }
    </style>
</head>
<body onload="docLoaded();">
    <div id="queryForm" class="title_bar">
        <template>          
            <div class="query_form">
                <el-form :inline="true" :model="searchData" class="demo-form-inline"> 
                    <el-form-item label="UserName">
                        <el-input v-model="searchData.userName" placeholder="User name"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">Query</el-button>
                    </el-form-item>                  
                </el-form>
            </div>       
            <div class="hline"></div>
            <div class="panel">               
                <el-table :data="tableData" style="width: 100%" max-height="660">
                    <el-table-column prop="id" label="ID" min-width="5%"></el-table-column>
                    <el-table-column prop="userName" label="User Name" min-width="15%"></el-table-column>
                    <el-table-column prop="accessKey" label="AccessKey" min-width="40%"></el-table-column>                 
                    <el-table-column prop="statusName" label="Status" min-width="10%"></el-table-column>
                    <el-table-column label="Operation" min-width="30%">
                        <template slot-scope="scope">                                      
                            <el-button @click="handleEdit(scope.row)" type="text" size="small">设置角色</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>   
            <div class="status_bar" style="text-align: left;"> 
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchData.pageNum"
                    :page-sizes="[5, 10, 15]"
                    :page-size="searchData.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageInfo.total">
                </el-pagination>       
            </div>
            <el-dialog :title="dialogTitle" :visible.sync="editDialogFormVisible" width="52%" top="10vh"> 
                <el-form :model="dialogForm" :label-width="formLabelWidth" size="mini"> 
                    <el-form-item label="User Name" >
                        <el-input v-model="dialogForm.userName" autocomplete="off" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="Role List">
                        <el-transfer
                        :titles="['未选', '已选']"
                        :button-texts="['移除', '添加']"
                        filterable                      
                        filter-placeholder="请输入Role Name"
                        v-model="roleData.checkRoles"
                        :data="roleData.roles">
                        </el-transfer>
                    </el-form-item>                  
                    <el-form-item label="Status">
                        <el-select v-model="dialogForm.status" placeholder="please select status">
                            <el-option v-for="item in statusList"
                            :key="item.status"
                            :label="item.name"
                            :value="item.status">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editDialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSubmit">确 定</el-button>
                </div>                
            </el-dialog>      
        </template>
    </div>
    <script type="text/javascript">
        var router = startPage();
        function docLoaded() {           
            var queryApp = new Vue({el: '#queryForm', data: {                                
                    searchData:{ 
                        userName: "",                       
                        pageNum:1,
                        pageSize:5
                    },
                    pageInfo:{
                        totalPage:0,
                        total:0
                    },                   
                    tableData:[],
                    statusList:[{status:0,name:"normal"},{status:1,name:"disable"}],
                    formLabelWidth: '120px',
                    editDialogFormVisible:false,                    
                    dialogTitle: "Edit UserRoles",   
                    dialogForm:{  
                        userId:0,
                        userName:"",  
                        roleList: [],                                          
                        status: 0
                    },           
                    roleData:{                       
                        roles: [],         
                        userRoles: [],
                        checkRoles: []           
                    },                                              
                },
                methods: {  
                    loadRoleList() {        
                        var searchData = {roleName:""};   
                        httpPost(apiRoleList(this,router.authInfo,JSON.stringify(searchData))).success(function(res,context){ 
                            var roleList = checkAndGetData(res);
                            if(!roleList) return;
                            roleList.forEach((item, index) => {
                                context.roleData.roles.push({
                                    label: item.roleName,
                                    key: item.id
                                });
                            });                                   
                        }).then(function(status, res, context){context.$message.error(res);});
                    },               
                    loadUserRoleList() {                             
                        httpPost(apiUserRoleList(this,router.authInfo,JSON.stringify(this.dialogForm))).success(function(res,context){ 
                            var userRoleList = checkAndGetData(res);
                            if(!userRoleList) return;      
                            userRoleList.forEach((item, index) => {
                                context.roleData.checkRoles.push(item.roleId);
                            });       
                            context.roleData.userRoles = userRoleList;
                            context.editDialogFormVisible = true;     
                        }).then(function(status, res, context){context.$message.error(res);});
                    },   
                    onSubmit:function(event){
                        this.searchData.pageNum = 1;
                        this.loadTableData(event);
                    },                     
                    loadTableData:function(event){                      
                        this.editDialogFormVisible = false;            
                        httpPost(apiUserPage(this,router.authInfo,JSON.stringify(this.searchData))).success(function(res,context){ 
                            parseAndCheckResult(context,res);
                        }).then(function(status, res, context){context.$message.error(res);});
                    },
                    handleSizeChange(val) {                     
                        this.searchData.pageSize = val;
                        this.loadTableData();
                    },
                    handleCurrentChange(val) {
                        this.searchData.pageNum = val;
                        this.loadTableData();
                    },                  
                    handleEdit(row){                   
                       this.dialogForm.userId = row.id;
                       this.dialogForm.userName = row.userName;
                       this.dialogForm.roleList = [];
                       this.roleData.checkRoles = []; 
                       this.loadUserRoleList();                                                           
                    },                            
                    editSubmit:function(event){ 
                        this.dialogForm.roleList = this.roleData.checkRoles;
                        var apiRequest = apiUserRoleUpdate(this,router.authInfo,JSON.stringify(this.dialogForm));
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);                                                      
                            context.loadTableData();
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    }
                }
            }); 
            queryApp.onSubmit();
            queryApp.loadRoleList();
            //init end.
        } 

        function parseAndCheckResult(vApp, res){
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            if(resObj.pageInfo){
                vApp.pageInfo.total = resObj.pageInfo.total;
                vApp.pageInfo.totalPage = resObj.pageInfo.totalPage;
            }
            if(resObj.data){                
                var arrayObj = new Array();
                for(var item of resObj.data){
                    arrayObj.push({
                        id:item.id,
                        userName:item.userName,                        
                        accessKey:item.accessKey,                                                           
                        enable:item.enable,
                        statusName:(item.enable)?"normal":"disable"});
                }
                vApp.tableData = arrayObj;
            }
            return resObj;
        }

        function checkResult(vApp, res, isErr){            
            if(isErr){
                vApp.$message.error(res);
                return;
            }
            var resObj = JSON.parse(res); 
            if(resObj.code == SUCCESS_CODE){
                vApp.$message({type: 'success', message: resObj.message});
                return;
            }
            vApp.$message.error(resObj.message+","+resObj.code);
        }

        function checkAndGetData(res){            
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            return resObj.data;
        }
    </script>
</body>
</html>
